#include "StdAfx.h"
#include "upload_file_thread.h"
#include <commondef/glb_msg_define.h>

upload_file_thread::upload_file_thread()
{
	this->ptr_http_client_ = NULL;
}

upload_file_thread::~upload_file_thread(void)
{
	if ( this->ptr_http_client_ )
	{
		delete this->ptr_http_client_;
		this->ptr_http_client_ = NULL;
	}
}

void 
upload_file_thread::ActualWorkerThreadProc(PWORKER_PARAMETER work_para)
{
	aot::tt::ievent_base* ptr_notify = this->session_setting_.ptr_notify;
	_AFT::isession * ptr_session = this->session_setting_.ptr_session;

	if ( NULL == this->ptr_http_client_ )
	{
		this->ptr_http_client_ = new http_client;/// handled del
	}

	int result = -1;
	if ( ptr_session->get_type() == _AFT::isession::e_direct_upload_file )
	{
		result = this->ptr_http_client_->upload_file(this->session_setting_, FALSE);
	}
	else if ( ptr_session->get_type() == _AFT::isession::e_upload_file_with_check )
	{
		result = this->ptr_http_client_->upload_file(this->session_setting_, TRUE);
	}

	ptr_session->set_result(result);

	if ( ptr_notify )
	{
		TRACE(_T("WM_GLB_FTC_TO_UI_FINISH: %s"), ptr_session->get_file_name());
		ptr_notify->on_notify(WM_GLB_FTC_TO_UI_FINISH, (WPARAM)ptr_session, NULL);
	}
}

BOOL 
upload_file_thread::DestroyThreads()
{
	if ( this->ptr_http_client_ )
	{
		this->ptr_http_client_->set_cancel();
	}
	
	WorkerThreads::DestroyThreads();

	if ( this->ptr_http_client_ )
	{
		this->ptr_http_client_->set_cancel(FALSE);
	}

	return TRUE;
}

void 
upload_file_thread::config_session(_AFT::isession* ptr_session, aot::tt::ievent_base* ptr_notify)
{
	this->session_setting_.ptr_session = ptr_session; 
	this->session_setting_.ptr_notify = ptr_notify;
}

/// basic check by fan05